﻿package Views
{
	import Events.SnakeChangeEvent;
	import Player.SnakeHead;	
	import flash.geom.Point;
	import Collision.CollisionChecker;
	import flash.display.Sprite;
	import flash.display.CapsStyle;
	import flash.display.JointStyle;

	/**
	 * Class to display a snake on screen
	 */
	public class SnakeView extends Sprite
	{		
		private var _field:Field;
		private const headOffSet:Number = 2;
		
		public function SnakeView(field:Field)
		{
			_field = field;
			
			// Add snake view to field
			_field.addSnake(this);
		}
		
		/**
		 * Function to draw the snake head
		 * Head can be Rectangular or round
		 * Head can be normal or inverted color
		 */
		public function updateSnakeHead(snakeChangeEvent:SnakeChangeEvent):void{
			var snake:SnakeHead = snakeChangeEvent.snake;
			
			// Check if snake can be drawed
			if(snake != null){			

				
				// Fill shape
				graphics.beginFill(snake.headColor);
				
				// Determine to draw round or rectangular head
				if(snake.roundHead){
					// Set linestyle for round head
					graphics.lineStyle(0,snake.headColor,Game.alpha,Game.pixelHinting,Game.scaleMode,Game.caps,Game.joints);
			
					//graphics.drawEllipse(radius,radius,radius,radius);
					graphics.drawEllipse(-(snake.thickness/2), -(snake.thickness/2),snake.thickness,snake.thickness);
				}else{
					// Set linestyle for square head
					graphics.lineStyle(1,snake.headColor,Game.alpha,Game.pixelHinting,Game.scaleMode,CapsStyle.NONE,JointStyle.MITER);
					graphics.drawRect(-(snake.thickness/2),-(snake.thickness/2),snake.thickness,snake.thickness);
				}
				graphics.endFill();
			}
		}
		
		/**
		 * Function to draw the colored tail on a snake
		 * after it has moved. The tail will be directly drawn on the field.
		 */
		public function updateAfterMove(snakeChangeEvent:SnakeChangeEvent):void{
			var snake:SnakeHead = snakeChangeEvent.snake;
			
			// Check if snake can be drawed
			if(snake != null){						
				// Get rotation angle of snake
				var angleInRadians : Number = Math.atan2(snake.orientation.y, snake.orientation.x);
				var andleInDegrees : Number = angleInRadians * (180 / Math.PI);
				
				this.rotation = andleInDegrees;
				
				// Update snake head position
				this.x = snake.position.x;
				this.y = snake.position.y;
			
				// Check for map collisions
				checkCollision(snakeChangeEvent);
				_field.updateSnake(snake, snakeChangeEvent.oldPosition);
			}
		}
		
		/**
		* Method to check for collisions. On collission
		* the collision Point will be returned. Otherwise the snake
		* position will be returned;
		*/
		private function checkCollision(snakeChangeEvent:SnakeChangeEvent):Boolean{
			var snake:SnakeHead = snakeChangeEvent.snake;			

			// Dispatch event on collision
			if(!CollisionChecker.isColliding(_field.collisionMap, this, _field, true))
			{
				// TODO, collision position
				dispatchEvent(new SnakeChangeEvent(SnakeChangeEvent.SNAKE_DEATH,snake,snake.position));
				return true;
			}
			return false;
		}
	}
}